package com.ztesoft.book.admin.config.shiro.handler;


import org.apache.shiro.aop.AnnotationResolver;
import org.apache.shiro.aop.MethodInvocation;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.aop.AuthorizingAnnotationHandler;
import org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor;


/**
 * @Description 自定义权限替换
 * @Author YangFan
 * @Date 2021-07-07 23:49
 */
public class MyPermissionAnnotationMethodInterceptor extends AuthorizingAnnotationMethodInterceptor {

    public MyPermissionAnnotationMethodInterceptor(AnnotationResolver resolver) {
        super(new MyPermissionAnnotationHandler(), resolver);
    }


    /**
     * 方法功能描述:
     * < 替换拦截器为自定义权限拦截器 >
     *
     * @param mi 【参数 1 注释说明】
     * @return void                            【返回值 注释说明】
     * @author YangFan
     * @date 2021-07-07 23:49
     */
    @Override
    public void assertAuthorized(MethodInvocation mi) throws AuthorizationException {
        try {
            AuthorizingAnnotationHandler handler = (AuthorizingAnnotationHandler) getHandler();
            if (handler instanceof MyPermissionAnnotationHandler) {
                ((MyPermissionAnnotationHandler) handler).assertAuthorized(mi);
            }
            else {
                handler.assertAuthorized(getAnnotation(mi));
            }
        }
        catch (AuthorizationException ae) {
            if (ae.getCause() == null) {
                ae.initCause(new AuthorizationException("Not authorized to invoke method: " + mi.getMethod()));
            }
            throw ae;
        }
    }
}
